<template>
  <el-dialog
    :title="!dataForm.moveId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品图片" prop="goodsImg">
      <el-input v-model="dataForm.goodsImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="用户ID" prop="userid">
      <el-input v-model="dataForm.userid" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="砍价ID" prop="barginId">
      <el-input v-model="dataForm.barginId" placeholder="砍价ID"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="显示状态" prop="xianshiStatis">
      <el-input v-model="dataForm.xianshiStatis" placeholder="显示状态"></el-input>
    </el-form-item>
    <el-form-item label="唯一标识" prop="weiyi">
      <el-input v-model="dataForm.weiyi" placeholder="唯一标识"></el-input>
    </el-form-item>
    <el-form-item label="开团人标识" prop="kaituanWeiyi">
      <el-input v-model="dataForm.kaituanWeiyi" placeholder="开团人标识"></el-input>
    </el-form-item>
    <el-form-item label="用户名称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="砍价状态" prop="moveStatus">
      <el-input v-model="dataForm.moveStatus" placeholder="砍价状态"></el-input>
    </el-form-item>
    <el-form-item label="邀请用户砍价次数" prop="kancount">
      <el-input v-model="dataForm.kancount" placeholder="邀请用户砍价次数"></el-input>
    </el-form-item>
    <el-form-item label="每次砍价剩余" prop="yukan">
      <el-input v-model="dataForm.yukan" placeholder="每次砍价剩余"></el-input>
    </el-form-item>
    <el-form-item label="砍的金额" prop="knayuyue">
      <el-input v-model="dataForm.knayuyue" placeholder="砍的金额"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="informa">
      <el-input v-model="dataForm.informa" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="每次最多砍" prop="kanmax">
      <el-input v-model="dataForm.kanmax" placeholder="每次最多砍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          moveId: 0,
          goodsImg: '',
          userid: '',
          barginId: '',
          goodsId: '',
          xianshiStatis: '',
          weiyi: '',
          kaituanWeiyi: '',
          userName: '',
          goodsName: '',
          moveStatus: '',
          kancount: '',
          yukan: '',
          knayuyue: '',
          informa: '',
          kanmax: ''
        },
        dataRule: {
          goodsImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          userid: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          barginId: [
            { required: true, message: '砍价ID不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          xianshiStatis: [
            { required: true, message: '显示状态不能为空', trigger: 'blur' }
          ],
          weiyi: [
            { required: true, message: '唯一标识不能为空', trigger: 'blur' }
          ],
          kaituanWeiyi: [
            { required: true, message: '开团人标识不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          moveStatus: [
            { required: true, message: '砍价状态不能为空', trigger: 'blur' }
          ],
          kancount: [
            { required: true, message: '邀请用户砍价次数不能为空', trigger: 'blur' }
          ],
          yukan: [
            { required: true, message: '每次砍价剩余不能为空', trigger: 'blur' }
          ],
          knayuyue: [
            { required: true, message: '砍的金额不能为空', trigger: 'blur' }
          ],
          informa: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          kanmax: [
            { required: true, message: '每次最多砍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.moveId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.moveId) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/barginhuodong/info/${this.dataForm.moveId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodsImg = data.barginHuodong.goodsImg
                this.dataForm.userid = data.barginHuodong.userid
                this.dataForm.barginId = data.barginHuodong.barginId
                this.dataForm.goodsId = data.barginHuodong.goodsId
                this.dataForm.xianshiStatis = data.barginHuodong.xianshiStatis
                this.dataForm.weiyi = data.barginHuodong.weiyi
                this.dataForm.kaituanWeiyi = data.barginHuodong.kaituanWeiyi
                this.dataForm.userName = data.barginHuodong.userName
                this.dataForm.goodsName = data.barginHuodong.goodsName
                this.dataForm.moveStatus = data.barginHuodong.moveStatus
                this.dataForm.kancount = data.barginHuodong.kancount
                this.dataForm.yukan = data.barginHuodong.yukan
                this.dataForm.knayuyue = data.barginHuodong.knayuyue
                this.dataForm.informa = data.barginHuodong.informa
                this.dataForm.kanmax = data.barginHuodong.kanmax
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/barginhuodong/${!this.dataForm.moveId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'moveId': this.dataForm.moveId || undefined,
                'goodsImg': this.dataForm.goodsImg,
                'userid': this.dataForm.userid,
                'barginId': this.dataForm.barginId,
                'goodsId': this.dataForm.goodsId,
                'xianshiStatis': this.dataForm.xianshiStatis,
                'weiyi': this.dataForm.weiyi,
                'kaituanWeiyi': this.dataForm.kaituanWeiyi,
                'userName': this.dataForm.userName,
                'goodsName': this.dataForm.goodsName,
                'moveStatus': this.dataForm.moveStatus,
                'kancount': this.dataForm.kancount,
                'yukan': this.dataForm.yukan,
                'knayuyue': this.dataForm.knayuyue,
                'informa': this.dataForm.informa,
                'kanmax': this.dataForm.kanmax
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
